package com.wxweven.algorithm.array.stackqueue;

import java.util.Stack;

/**
 * @author wxweven
 */
public class LC用栈实现队列 {

    private Stack<Integer> s1 = new Stack<>();
    private Stack<Integer> s2 = new Stack<>();

    /**
     * 添加元素到队尾
     */
    public void add(int x) {
        s1.push(x);
    }

    /**
     * 删除队头的元素并返回
     */
    public int remove() {
        top();
        return s2.pop();
    }

    /**
     * 返回队头元素
     */
    public int top() {
        if (!s2.isEmpty()) {
            return s2.peek();
        }

        while (!s1.isEmpty()) {
            s2.push(s1.pop());
        }

        return s2.peek();
    }

    /**
     * 判断队列是否为空
     */
    public boolean empty() {
        return s1.isEmpty() && s2.isEmpty();
    }
}